// pages/confirm/confirm.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,//底部弹框
        bottomcheck: false,//弹出框单选框初始值,
        payShow: true,//支付
        who: 0,//确定吊起哪个弹框
        boxChecked: false,//复选框
        userInfo: null,//购票人列表
        result: [],//点击新增购票人的复选框
        selectedPeople: [],//已经选择的购票人
        pay: [1, 2, 3],//1余额2支付包3微信
        radio: '1',
        // couponResult:['a', 'b'],//优惠券result
        couponArray: [],//点击优惠券的复选框
        coupon: [
            {
                id: "1",
                image: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg95.699pic.com%2Fdesgin_photo%2F40036%2F0679_detail.jpg%21detail860%2Ffw%2F820%2Fcrop%2F0x1309a0a0%2Fquality%2F90&refer=http%3A%2F%2Fimg95.699pic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641351497&t=46a36363ed908c148c5a2708efc2a4e2',
                title: "山东文旅惠民消费季5元",
                apply: "枣庄市通用",
                time: "2020-08-28 09:00",
            },
            {
                id: "2",
                image: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic199.nipic.com%2Ffile%2F20181227%2F25632018_112336421000_2.jpg&refer=http%3A%2F%2Fpic199.nipic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641351497&t=177bd20e9c0a5f0435aa093feaf02263',
                title: "山东文旅惠民消费季5元",
                apply: "枣庄市通用",
                time: "2020-08-28 09:00",
            }
        ]//优惠券列表
    },
    //获取选择时间页面传来的值
    onLoad(option) {
        console.log(option);
        let {date,day,fiel,time} = option
    },
    onShow() {
        //每次进来给userInfo赋值
        let userInfo = wx.getStorageSync('userInfo')
        this.setData({
            userInfo
        })
    },
    //点击优惠券的复选框
    couponResult(event) {
        var that = this
        var array = event.detail
        this.setData({
            couponArray: array,
        });
        console.log(event);
    },
    //获取支付的方式 1余额2支付包3微信
    payWay(event) {
        this.setData({
            radio: event.detail,
        });
    },
    payOne(event) {
        const { name } = event.currentTarget.dataset;
        this.setData({
            radio: name,
        });
        console.log(event.target.dataset.name);
    },
    //点击新增购票人的复选框
    payPeople(event) {
        var that = this
        var array = event.detail
        that.setData({
            result: array
        })
    },
    //确定购票人
    addPeople() {
        let len = this.data.result.length //result的长度
        let event = this.data.result //点击购票人的数组
        let user = this.data.userInfo
        console.log(user);
        console.log(event);
        console.log(len, "已选择购票人的长度");
        console.log(this.data.selectedPeople);
        for (var m = 0; m < len; m++) {
            let only = this.data.selectedPeople.findIndex((item, index) => {
                return item.phone == event[m]
            })
            if (only != -1) {
                Toast('勾选重复!!!');
                return
            }
        }
        for (var i = 0; i < len; i++) {
            let ind = user.findIndex((item, index) => {
                return item.phone == event[i]
            })
            console.log(ind);
            if (ind != -1) {
                this.data.selectedPeople.push(this.data.userInfo[ind])
                this.setData({
                    selectedPeople: this.data.selectedPeople,
                    show: false,
                    result: []
                })
            }
        }
    },
    // 点击编辑跳到bookpeople页面
    edit(e) {
        let index = e.currentTarget.dataset.index
        wx.navigateTo({
            url: `../bookpeople/bookpeople?index=` + index,
        })
    },
    //从bookpeople页面返回 关闭下弹框
    onHide() {
        this.setData({
            show: false
        })
    },
    //点击新增
    newIncrease() {
        let who = 1
        this.setData({
            show: true,
            who
        })
    },
    //跳转到新客户页面
    toBookpeople(e) {
        let number = e.currentTarget.dataset.number
        wx.navigateTo({
            url: '../bookpeople/bookpeople?number=' + number
        })
    },
    //点击新增弹框里面的复选框
    newChange(e) {
        let newChecked = e.detail
        this.setData({
            newChecked
        })
    },
    //返回上一个界面
    reture() {
        console.log(1);
        wx.navigateBack({
            delta: 1,
        })
    },
    //跳转到支付成功页面
    toPaymeny() {
        wx.navigateTo({
            url: '../payment/payment',
        })
    },
    // 优惠券复选框
    onBottomChange(e) {
        let bottomcheck = e.detail
        this.setData({
            bottomcheck
        })
    },
    // 点击优惠券弹起优惠券列表
    chooseBgc() {
        let who = 2
        this.setData({
            show: true,
            who
        })
    },
    // 支付关闭
    onPayClose() {
        this.setData({
            payShow: false
        })
    },
    // 优惠券关闭
    onClose() {
        this.setData({
            show: false,
            result: []
        })
    },
    //底部确定
    determine() {
        if (this.data.selectedPeople.length == 0) {
            wx.showToast({
                title: '请添加一位购票人信息',
                icon: "none"
            })
            return
        }
        let who = 3
        this.setData({
            who,
            show: true,
            payShow: true
        })
    },
    // 单选框
    onChange(e) {
        let checked = e.detail
        this.setData({
            checked,
            boxChecked: checked
        })
    }
})